﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Idlesoft.Cli;

namespace TestApp
{
	// 2)  Start by defining a class with custom cli attributes (CliAttribute or CliElement)
	public class Person
	{
		[CliAttribute(Description = "your name?")]
		public string Name { get; set; }

		[CliAttribute(Description = "your age?")]
		public int Age { get; set; }

		[CliElement(Description = "list of pets?")]
		public string[] Pets { get; set; }
	}
	public class Calculator
	{
		public Calculator() : this("") { }
		public Calculator(string name)
		{
			Name = name;
			_Ops["+"] = (x, y) => x + y;
			_Ops["-"] = (x, y) => x - y;
			_Ops["*"] = (x, y) => x * y;
		}
		[CliElement(Required = true, Description = "list of numbers to perform the Op on")]
		public int[] Numbers { get; set; }

		[CliAttribute(Description = "operation to be used; (+, -, *) are valid", Default = "+")]
		public string Op { get; set; }

		public string Name { get; set; }

		public int Calculate()
		{
			int result = Numbers.Aggregate(_Ops[Op]);
			return result;
		}
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}
		public override bool Equals(object obj)
		{
			var rhs = obj as Calculator;
			bool equals = Op.Equals(rhs.Op) && Name.Equals(rhs.Name);

			if (equals)
			{
				if (Numbers != null && rhs.Numbers != null && Numbers.Length == rhs.Numbers.Length)
				{
					string str1 = Numbers.Aggregate("", (curr, next) => curr + "," + next);
					string str2 = rhs.Numbers.Aggregate("", (curr, next) => curr + "," + next);
					equals = str1.Equals(str2);
				}
				else if (Numbers == null && rhs.Numbers == null)
					equals = true;
				else
					equals = false;
			}

			return equals;
		}

		private Dictionary<string, Func<int, int, int>> _Ops = new Dictionary<string, Func<int, int, int>>();
	}
}
